package br.com.tabletcaixa.numero.saidainterna;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import br.com.tabletcaixa.db.DBHelper;
import br.com.tabletcaixa.numero.venda.NumeroVendaVO;

public class NumeroSaidaInternaDAO {

	private SQLiteDatabase db;
	private DBHelper dbHelper;

	private String[] colunas = { DBHelper.NUMERO_SAIDA_INTERNA_ID,
			DBHelper.NUMERO_SAIDA_INTERNA_DESCRICAO };

	public NumeroSaidaInternaDAO(Context context) {

		dbHelper = new DBHelper(context);

	}

	public void conectar() {

		db = dbHelper.getWritableDatabase();

	}

	public void desconectar() {

		dbHelper.close();

	}

	public long cadastrarNumeroSaidaInterna(NumeroSaidaInternaVO numeroSaidaInterna) {

		try {

			ContentValues values = new ContentValues();
			values.put(DBHelper.NUMERO_SAIDA_INTERNA_DESCRICAO, numeroSaidaInterna.getDescricao());

			return db.insert(DBHelper.TBL_NUMERO_SAIDA_INTERNA, null, values);

		} catch (Exception e) {

			e.printStackTrace();
			e.getMessage();

		}

		return 0;

	}

	public long atualizarNumeroSaidaInterna(NumeroSaidaInternaVO numeroSaidaInterna) {

		try {

			long id = numeroSaidaInterna.getId();
			
			ContentValues values = new ContentValues();
			values.put(DBHelper.NUMERO_SAIDA_INTERNA_DESCRICAO, numeroSaidaInterna.getDescricao());

			return db.update(DBHelper.TBL_NUMERO_SAIDA_INTERNA, values, DBHelper.NUMERO_SAIDA_INTERNA_ID + " = " + id, null);

		} catch (Exception e) {

			e.printStackTrace();
			e.getMessage();

		}

		return 0;

	}
	
	public List<NumeroSaidaInternaVO> buscarNumeroSaidaInterna(){
		
		
		List<NumeroSaidaInternaVO> lstNumeroSaidaInterna = new ArrayList<NumeroSaidaInternaVO>();
		
		Cursor cursor = db.query(DBHelper.TBL_NUMERO_SAIDA_INTERNA, colunas, 
				null, null, null, null, DBHelper.NUMERO_SAIDA_INTERNA_DESCRICAO);
		
		cursor.moveToFirst();
		
		while(!cursor.isAfterLast()){
			
			NumeroSaidaInternaVO lNumeroSaidaInterna = cursorToNumeroSaidaInterna(cursor);
			lstNumeroSaidaInterna.add(lNumeroSaidaInterna);
			cursor.moveToNext();
			
		}
		
		cursor.close();
		return lstNumeroSaidaInterna;
		
	}
	
	private NumeroSaidaInternaVO cursorToNumeroSaidaInterna(Cursor cursor){
		
		NumeroSaidaInternaVO numeroSaidaInterna = new NumeroSaidaInternaVO();
		numeroSaidaInterna.setId(cursor.getLong(0));
		numeroSaidaInterna.setDescricao(cursor.getString(1));
		
		return numeroSaidaInterna;
		
	}
	
}
